package com.example.kai.uno.guide;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.kai.uno.Login;
import com.example.kai.uno.R;

import java.util.ArrayList;
import java.util.List;

public class guide_page1 extends AppCompatActivity {
    private ViewPager viewPager;//对应的viewpager
    private Button btnStart;// 进入

    private static  int[] mImageIds = {R.drawable.guide_page1,R.drawable.guide_page2, R.drawable.guide_page4,R.drawable.guide_page5};
    private ArrayList<ImageView>  mImageViewList;

    private LinearLayout llPointGroup;// 引导圆点的父控件
    private int mPointWidth;// 圆点间的距离
    private View viewPoint;// 灰点

    List<View> viewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.guide_page1);

        viewPager = (ViewPager)findViewById(R.id.vp_guide);
        llPointGroup = (LinearLayout) findViewById(R.id.guide_point);
        viewPoint = (View) findViewById(R.id.view_point);
        btnStart = (Button) findViewById(R.id.btn_start);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(guide_page1.this,"尽情享受游戏吧",Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(guide_page1.this,Login.class);
                startActivity(intent);
            }
        });


        //把变量与布局联系起来
        //这里的LayoutInflater系列方法是把xml文件转换成view对象
//        LayoutInflater layoutInflater = getLayoutInflater();
//        view1 = layoutInflater.inflate(R.layout.guide_page2,null);
//        view2 = layoutInflater.inflate(R.layout.guide_page3,null);
//        view3 = layoutInflater.inflate(R.layout.guide_page4,null);
//        view4 = layoutInflater.inflate(R.layout.guide_page5,null);
//
//        viewList = new ArrayList<View>();//把4个view加载到viewlist中
//        viewList.add(view1);
//        viewList.add(view2);
//        viewList.add(view3);
//        viewList.add(view4);

        initViews();
        viewPager.setAdapter(new GuideAdapter());

       viewPager.setOnPageChangeListener(new GuidePageListener());

    }

    /**
     * 初始化界面
     */
    private void initViews() {
        mImageViewList = new ArrayList<ImageView>();

        // 初始化引导页的4个页面
        for (int i = 0; i < mImageIds.length; i++) {
            ImageView image = new ImageView(this);
            image.setBackgroundResource(mImageIds[i]);// 设置引导页背景
            mImageViewList.add(image);
        }

        // 初始化引导页的灰色圆点
        for (int i = 0; i < mImageIds.length; i++) {
            View point = new View(this);
            point.setBackgroundResource(R.drawable.dot_normal);// 设置引导页默认圆点

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(16, 16);
            if (i > 0) {
                params.leftMargin = 16;// 设置圆点间隔
            }

            point.setLayoutParams(params);// 设置圆点的大小

            llPointGroup.addView(point);// 将圆点添加给线性布局
        }

        // 获取视图树, 对layout结束事件进行监听
        llPointGroup.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    // 当layout执行结束后回调此方法
                    @Override
                    public void onGlobalLayout() {
                        llPointGroup.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        mPointWidth = llPointGroup.getChildAt(1).getLeft()-llPointGroup.getChildAt(0).getLeft();
                    }
                });
    }

    /**
     * ViewPager数据适配器
     *
     */
    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mImageViewList.get(position));
            return mImageViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    /**
     * viewpager的滑动监听
     */
    class GuidePageListener implements ViewPager.OnPageChangeListener {

        // 滑动事件
        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            // System.out.println("当前位置:" + position + ";百分比:" + positionOffset
            // + ";移动距离:" + positionOffsetPixels);
            int len = (int) (mPointWidth * positionOffset) + position * mPointWidth;
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewPoint.getLayoutParams();// 获取当前蓝点的布局参数
            params.leftMargin = len;// 设置左边距
            viewPoint.setLayoutParams(params);// 重新给灰点设置布局参数
        }

        // 某个页面被选中
        @Override
        public void onPageSelected(int position) {
            if (position == mImageIds.length - 1) {// 最后一个页面
                btnStart.setVisibility(View.VISIBLE);// 显示立即进入的按钮
            } else {
                btnStart.setVisibility(View.INVISIBLE);
            }
        }

        // 滑动状态发生变化
        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }

}
